package nccloud.web.uapbd.ref.org.alldata;

import java.util.List;
import java.util.Map;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.jdbc.framework.processor.MapListProcessor;
import nccloud.framework.web.container.SessionContext;
import nccloud.framework.web.processor.refgrid.RefQueryInfo;
import nccloud.framework.web.ui.meta.RefMeta;
import nccloud.framework.web.ui.meta.TreeRefMeta;
import nccloud.pubitf.platform.db.SqlParameterCollection;
import nccloud.web.refer.DefaultTreeRefAction;

public class BusinessUnitAndDeptAllTreeRef extends DefaultTreeRefAction {
	public BusinessUnitAndDeptAllTreeRef() {
		this.setShowDisabledData(true);
		this.setResourceCode("org");
		this.setUnitPkKey("pk_group");
	}

	public TreeRefMeta getRefMeta(RefQueryInfo refQueryInfo) {
		TreeRefMeta refMeta = new TreeRefMeta();
		refMeta.setCodeField("code");
		refMeta.setNameField("name");
		refMeta.setMutilLangNameRef(true);
		refMeta.setPidField("pk_unionfather");
		refMeta.setPkField("pk_org");
		refMeta.setTableName(
				"(SELECT  dr,code, name, name2, name3, name4, name5, name6, pk_org, pk_fatherorg, pk_fatherorg AS pk_unionfather, pk_group, enablestate, orgtype17,isbusinessunit FROM org_orgs WHERE isbusinessunit = 'Y' UNION SELECT dr,code,name , name2, name3, name4, name5, name6, pk_dept AS pk_org, pk_fatherorg, CASE WHEN pk_fatherorg = '~' THEN pk_org ELSE pk_fatherorg END AS pk_unionfather, pk_group, enablestate, orgtype17,'N' FROM org_dept) org_businessunitanddept_temp");
		return refMeta;
	}

	public String getExtraSql(RefQueryInfo refQueryInfo, RefMeta refMeta) {
		// 当为单据查询节点时，组织为当前登录人组织，部门需要限定在当前登录人部门及其下级部门。
		String appcode = SessionContext.getInstance().getAppcode();
		String deptid = "";
		if ("201104DJCX".equals(appcode)) {
			String userid = InvocationInfoProxy.getInstance().getUserId();
			return " and pk_group = ? and pk_org in (SELECT " + 
					"	pk_dept " + 
					"FROM " + 
					"	org_dept " + 
					"CONNECT BY " + 
					"	PRIOR pk_dept =  pk_fatherorg " + 
					"START WITH " + 
					"	pk_dept IN ( " + 
					"	SELECT " + 
					"		pk_dept " + 
					"	FROM " + 
					"		bd_psnjob " + 
					"	WHERE " + 
					"		indutydate < to_char(sysdate, 'yyyy-mm-dd') " + 
					"			AND (enddutydate IS NULL " + 
					"				OR enddutydate > to_char(sysdate, 'yyyy-mm-dd')) " + 
					"				AND pk_psndoc IN ( " + 
					"				SELECT " + 
					"					pk_psndoc " + 
					"				FROM " + 
					"					sm_user " + 
					"				WHERE " + 
					"					cuserid = '"+userid+"'))) " ;

		
		}
		// end

		return " and pk_group = ? ";
	}

	public SqlParameterCollection getExtraSqlParameter(RefQueryInfo paramRefQueryInfo, RefMeta paramRefMeta) {
		String strpk_group = this.getPk_group(paramRefQueryInfo);
		SqlParameterCollection collParam = new SqlParameterCollection();
		collParam.addChar(strpk_group);
		return collParam;
	}
}